home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Palm FlyingToastersScreenSaver / Src / SampleHack.cpp
Encoding:
C/C++ Source or Header  |  2000-06-24  |  6.9 KB  |  271 lines

  1. /*************************************************************************
  2.  *
  3.  * PROJECT:  SampleHack
  4.  * FILE:     SampleHack
  5.  *
  6.  * DESCRIPTION:
  7.  *      A small, simple hack for demonstration at MacHack '99.
  8.  *      Rip it apart, make something new out of it, and put it back together.
  9.  *      All before the hack contest...
  10.  *      
  11.  **********************************************************************/
  12.  
  13. #include <Pilot.h>
  14. #include <SysEvtMgr.h>
  15.  
  16. /*
  17.     List of Stuff:
  18.     • Make a routine with the same parameters & return type as the 
  19.         one you're patching.  Here its "MySysHandleEvent".
  20.     • Make it the "Entry Point" for the code resource ("68k Target" settings)
  21.     • Put the trap ID of the trap you're patching in the "PalmRez Post Linker" 
  22.         settings panel.  Get this from the hacktrap.txt file.
  23.     • Change the name of the hack ('tAIN' resource ID 3000)
  24.     • Change the about box ('tFRM' ID 3000 - edit this with Constructor for PalmOS)
  25. */
  26.  
  27.  
  28. /***********************************************************************
  29.  *
  30.  *   Global variables
  31.  *
  32.  ***********************************************************************/
  33.  
  34. /* 
  35.     We allocate a memory chunk to store our globals in and keep the ptr
  36.     stored in a feature. The PalmOS FeatureMgr is similar to MacOS's Gestalt.
  37.     
  38.     Here is the structure:
  39. */
  40.  
  41. typedef struct
  42. {
  43.     short stringPos;
  44. }PatchGlobals, *PatchGlobalsPtr;
  45.  
  46. /***********************************************************************
  47.  *
  48.  *   Internal Constants
  49.  *
  50.  ***********************************************************************/
  51. #define kHackFileCreator        'DARK'
  52. #define kHackCodeID                1000
  53. #define kGlobalsPtrID            1100
  54.  
  55. #define kTargetString            "MacHack"
  56.  
  57.  
  58. /***********************************************************************
  59.  *
  60.  *   Function Prototypes
  61.  *
  62.  ***********************************************************************/
  63.  
  64. extern "C" Boolean    MySysHandleEvent(EventPtr eventP);
  65. PatchGlobalsPtr GetGlobals(void);
  66. void ScreenSaver(void);
  67.  
  68.  
  69. void ScreenSaver(void)
  70. {
  71.     RectangleType    screenRect = {{0,0},{160,160}};
  72.     DmOpenRef        db;
  73.     Handle            pictHandle;
  74.     BitmapPtr        pictPtr;
  75.     CustomPatternType    pattern = { 0xffff, 0xffff, 0xffff, 0xffff};
  76.     
  77.     WinHandle win = WinGetActiveWindow();
  78.     Word error;
  79.     WinHandle off = WinCreateOffscreenWindow(160, 160, screenFormat, &error);
  80.     WinCopyRectangle(win, off, &screenRect, 0, 0, scrCopy);
  81.  
  82.     WinSetPattern(pattern);
  83.     WinFillRectangle(&screenRect, 0);
  84.     
  85.     
  86.     db = DmOpenDatabaseByTypeCreator('HACK', kHackFileCreator, dmModeReadOnly);
  87.     
  88.     //SndPlaySmfResource('Midi', 128, prefSysSoundVolume);
  89.     
  90.     pictHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 128));
  91.     pictPtr = static_cast<BitmapPtr>(MemHandleLock(pictHandle));
  92.     WinDrawBitmap(pictPtr, 50, 50);
  93.     
  94.     Handle        middleHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 128));
  95.     BitmapPtr    middlePtr = static_cast<BitmapPtr>(MemHandleLock(middleHandle));
  96.  
  97.     Handle        downHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 129));
  98.     BitmapPtr    downPtr = static_cast<BitmapPtr>(MemHandleLock(downHandle));
  99.  
  100.     Handle        upHandle = reinterpret_cast<Handle>(DmGetResource(bitmapRsc, 130));
  101.     BitmapPtr    upPtr = static_cast<BitmapPtr>(MemHandleLock(upHandle));
  102.     
  103.     BitmapPtr    states[4] = {
  104.         middlePtr,
  105.         downPtr,
  106.         middlePtr,
  107.         upPtr,
  108.     };
  109.     
  110.     int x1 = 160;
  111.     int y1 = 10;
  112.     int x2 = 160;
  113.     int y2 = 60;
  114.     
  115.     for (int i = 0; i != 254; ++i) {
  116.         WinFillRectangle(&screenRect, 0);
  117.         WinDrawBitmap(states[i % 4], x1, y1);
  118.         WinDrawBitmap(states[(i + 1) % 4], x2, y2);
  119.         SysTaskDelay(10);
  120.         x1 -= 2;
  121.         y1 += 1;
  122.         x2 -= 4;
  123.         y2 += 2;
  124.         if (x1 < 0) {
  125.             x1 = 160;
  126.         }
  127.         if (x1 > 160) {
  128.             x1 = 0;
  129.         }
  130.         if (x2 < 0) {
  131.             x2 = 160;
  132.         }
  133.         if (x2 > 160) {
  134.             x2 = 0;
  135.         }
  136.         if (y1 < 0) {
  137.             y1 = 160;
  138.         }
  139.         if (y1 > 160) {
  140.             y1 = 0;
  141.         }
  142.         if (y2 < 0) {
  143.             y2 = 160;
  144.         }
  145.         if (y2 > 160) {
  146.             y2 = 0;
  147.         }
  148.         
  149.     }
  150.     
  151.     MemHandleUnlock(upHandle);
  152.     MemHandleUnlock(downHandle);
  153.     MemHandleUnlock(middleHandle);
  154.     DmReleaseResource(upHandle);
  155.     DmReleaseResource(downHandle);
  156.     DmReleaseResource(middleHandle);
  157.     
  158.     DmCloseDatabase(db);
  159.     
  160.     WinEraseRectangle(&screenRect, 0);
  161.     WinCopyRectangle(off, win, &screenRect, 0, 0, scrCopy);
  162.     WinDeleteWindow(off, false);
  163.  
  164.  
  165. }
  166.  
  167.  
  168.  
  169. /*
  170.     This routine is theErr         SysUIAppSwitch(UInt cardNo, LocalID dbID, Word cmd, Ptr cmdPBP)
  171.                             SYS_TRAP(sysTrapSysUIAppSwitch);
  172.                             
  173. Err         SysCurAppDatabase(UIntPtr cardNoP, LocalID* dbIDP)
  174.                             SYS_TRAP(sysTrapSysCurAppDatabase);
  175.  entry point to the code resource and will be
  176.     called instead of SysHandleEvent.  It watches for keyDown events,
  177.     and if the user enters "MacHack", it will flash the screen.
  178.     
  179.     If you want to patch a different trap, replace this routine with your own.
  180.     Don't forget to update the "Entry Point" field in the "68K Target" settings 
  181.     panel to use the new routine!
  182.     
  183.     To tell HackMaster which trap to install your patch on, you must enter the 
  184.     trap ID in the "Trap IDs" field of the "PalmRez Post Linker" settings panel.
  185.     See the file "hacktrap.txt" for a list of traps and their ID's.
  186. */
  187. Boolean    MySysHandleEvent(EventPtr eventP)
  188. {
  189.     Boolean (*oldtrap)(EventPtr); // procedure pointer to old trap;
  190.     DWord temp; // for the feature manager call type checking
  191.     Boolean handled = false;
  192.     //PatchGlobalsPtr globalsP; 
  193.     Err err;
  194.     
  195.     // Get the address of the old routine from HackMaster:
  196.     err = FtrGet(kHackFileCreator,kHackCodeID,&temp);
  197.     ErrFatalDisplayIf(err, "can't get real trap address");
  198.     oldtrap=(Boolean (*)(EventPtr))temp;
  199.     
  200.     switch (eventP -> eType) {
  201.     case keyDownEvent:
  202.         if ((eventP -> data.keyDown.chr == vchrAutoOff) && (eventP -> data.keyDown.modifiers & commandKeyMask)) {
  203.             
  204.             ScreenSaver();
  205.             EvtResetAutoOffTimer();
  206.         
  207.         } else if ((eventP -> data.keyDown.chr == vchrRonamatic) && (eventP -> data.keyDown.modifiers & commandKeyMask)) {
  208.             ScreenSaver();
  209.             EvtResetAutoOffTimer();
  210.         }
  211.         break;
  212.     }
  213.     
  214.     if (!handled) {
  215.         handled = oldtrap(eventP);
  216.     }    
  217.     
  218.     return handled;
  219. }
  220.  
  221. #ifdef    SILLY
  222.     UInt        cardNo;
  223.     LocalID    dbID;
  224.             if (!SysCurAppDatabase(&cardNo, &dbID)) {
  225.                 SysUIAppSwitch(cardNo, dbID, sysAppLaunchCmdNormalLaunch, NULL);
  226.             }
  227. #endif
  228.  
  229. /*
  230.     Function to retrieve our globals from a feature.
  231.     Creates & intializes them if necessary.
  232.     Returns ptr to globals, or 0 if something goes awry.
  233. */
  234. PatchGlobalsPtr GetGlobals(void)
  235. {
  236.     PatchGlobalsPtr globalsP;
  237.     Err err;
  238.     
  239.     err = FtrGet(kHackFileCreator, kGlobalsPtrID, (unsigned long*)&globalsP);
  240.     
  241.     // Create & initialize our globals if they don't exist yet.
  242.     if(err == ftrErrNoSuchFeature)
  243.     {
  244.         globalsP = static_cast<PatchGlobalsPtr>(MemPtrNew(sizeof(PatchGlobals)));
  245.  
  246.         if(globalsP != 0)
  247.         {
  248.             // Set its owner to 0 so it isn't automatically freed
  249.             err = MemPtrSetOwner(globalsP, 0);
  250.             ErrFatalDisplayIf(err, "MemPtrSetOwner failed");
  251.  
  252.             // initialize globals structure:
  253.             globalsP->stringPos = 0;
  254.             
  255.             
  256.             // Save our gloabls ptr in a feature so we can use it next time
  257.             FtrSet(kHackFileCreator, kGlobalsPtrID, (unsigned long)globalsP);
  258.             
  259.             // clear error so we know everything's okay.
  260.             err = 0;
  261.         }
  262.     }
  263.     
  264.     // if we got our globals (one way or another)
  265.     // then return them.
  266.     if(err == 0) return globalsP;
  267.     
  268.     return 0;
  269. }
  270.  
  271.